package com.mrdeer.web.service.sys.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrdeer.common.entity.beans.PageEntity;
import com.mrdeer.common.entity.dto.DepartmentHeaderDTO;
import com.mrdeer.common.entity.dto.UserDTO;
import com.mrdeer.common.entity.sys.DepartmentHeader;
import com.mrdeer.common.entity.sys.User;
import com.mrdeer.common.exception.BusinessException;
import com.mrdeer.web.mapper.DepartmentHeaderMapper;
import com.mrdeer.web.mapper.SysUserMapper;
import com.mrdeer.web.service.sys.SysDepartmentHeaderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class SysDepartmentHeaderServiceImpl extends ServiceImpl<DepartmentHeaderMapper, DepartmentHeader> implements SysDepartmentHeaderService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private DepartmentHeaderMapper departmentHeaderMapper;

    @Override
    public DepartmentHeader add(DepartmentHeader departmentHeader) {
        DepartmentHeader dh = getById(departmentHeader.getId());
        if (null != dh) {
            throw new BusinessException("the department header exists in the database");
        }
        if (departmentHeaderMapper.insert(departmentHeader) > 0) {
            return departmentHeaderMapper.selectById(departmentHeader.getId());
        }
        throw new BusinessException("The department header add failed");
    }

    @Override
    public boolean delete(String id) {
        DepartmentHeader dh = getById(id);
        if (null == dh) {
            throw new BusinessException("the department header does not exists");
        }
        return departmentHeaderMapper.deleteById(id) > 0;
    }

    @Override
    public DepartmentHeader update(DepartmentHeader departmentHeader) {
        if (departmentHeaderMapper.updateById(departmentHeader) > 0) {
            DepartmentHeader select = departmentHeaderMapper.selectById(departmentHeader.getId());
            return select;
        }
        throw new BusinessException("update department header failed");
    }

    @Override
    public DepartmentHeader getById(String id) {
        return departmentHeaderMapper.selectById(id);
    }

    @Override
    public Page<DepartmentHeader> list(DepartmentHeaderDTO dto) {
        if (null == dto.getPage()) {
            dto.setPage(new PageEntity());
        }
        return departmentHeaderMapper.selectPage(dto.getPage().convert(), fuzzyQuery(dto));
    }

    @Override
    public LambdaQueryWrapper<DepartmentHeader> fuzzyQuery(DepartmentHeaderDTO dto) {
        LambdaQueryWrapper<DepartmentHeader> queryWrapper = Wrappers.<DepartmentHeader>query().lambda();
        if (null != dto) {
            if (StrUtil.isNotBlank(dto.getUserId())) {
                queryWrapper.eq(DepartmentHeader::getUserId, dto.getUserId());
            }
            if (StrUtil.isNotBlank(dto.getDepartmentId())) {
                queryWrapper.eq(DepartmentHeader::getDepartmentId, dto.getDepartmentId());
            }
            if (dto.getType() != null) {
                queryWrapper.eq(DepartmentHeader::getType, dto.getType());
            }

        }
        return queryWrapper;
    }

    @Override
    public List<UserDTO> findHeaderByDepartmentId(String departmentId, Integer type) {
        List<UserDTO> list = new ArrayList<>();
        DepartmentHeaderDTO departmentHeaderDTO = new DepartmentHeaderDTO();
        departmentHeaderDTO.setDepartmentId(departmentId);
        departmentHeaderDTO.setType(type);
        List<DepartmentHeader> headers = departmentHeaderMapper.selectList(fuzzyQuery(departmentHeaderDTO));
        headers.forEach(e -> {
            User u = sysUserMapper.selectById(e.getUserId());
            if (u != null) {
                list.add(new UserDTO());
            }
        });
        return list;
    }

    @Override
    public List<DepartmentHeader> findByDepartmentIdIn(List<String> departmentIds) {
        return departmentHeaderMapper.selectList(new QueryWrapper<DepartmentHeader>().in("department_id", departmentIds));
    }

    @Override
    public void deleteByDepartmentId(String departmentId) {
        departmentHeaderMapper.deleteById(departmentId);
    }

    @Override
    public void deleteByUserId(String userId) {
        departmentHeaderMapper.delete(new LambdaQueryWrapper<DepartmentHeader>().eq(DepartmentHeader::getUserId, userId));
    }
}
